# options
set shellopts "-eu"
set ifs "\n"
set incsearch true
set scrolloff 8
set sixel true
set previewer ~/.config/lf/preview

# opener
cmd open &{{
    case "$(file --mime-type -bL "$f")" in
        text/* | application/json | inode/x-empty) lf -remote "send $id \$$EDITOR \$fx" ;;
        audio/* | video/*) mpv --player-operation-mode=pseudo-gui $fx ;;
        image/*) imv-dir "$f" ;;
        application/pdf | application/epub+zip) zathura "$f" ;;
        *)
            for f in $fx; do
            xdg-open "$f" > /dev/null 2>&1 &
            done
            ;;
    esac
}}

# open with
cmd open-with-cli $"$@" $fx
cmd open-with-gui &"$@" $fx

# bulk rename
cmd bulk-rename ${{
    old="$(mktemp)"
    new="$(mktemp)"
    if [ -n "$fs" ]; then
        fs="$(basename -a $fs)"
    else
        fs="$(ls)"
    fi
    printf "%s\n" "$fs" > "$old"
    printf "%s\n" "$fs" > "$new"
    "$EDITOR" "$new"
    if [ "$(wc -l < "$new")" -ne "$(wc -l < "$old")" ]; then
        exit
    fi
    paste "$old" "$new" | while IFS= read -r names; do
        src="$(printf "%s" "$names" | cut -f 1)"
        dst="$(printf "%s" "$names" | cut -f 2)"
        if [ "$src" = "$dst" ] || [ -e "$dst" ]; then
            continue
        fi
        mv "$src" "$dst"
    done
    rm "$old" "$new"
    lf -remote "send $id unselect"
}}

# trash
cmd trash &{{
    files="$HOME/.local/share/Trash/files"
    info="$HOME/.local/share/Trash/info"
    mkdir -p "$files" "$info"
    for f in $fx; do
        trash="$files/${f##*/}"
        trashinfo="$info/${f##*/}.trashinfo"
        if [ ! "$f" = "$trash" ]; then
            i=1
            while [ -e "$trash" ]; do
                trash="$files/${f##*/}.~$i~"
                trashinfo="$info/${f##*/}.~$i~.trashinfo"
                i=$((i + 1))
            done
            mv "$f" "$trash"
            printf "[TrashInfo]\nPath=%s\nDeletionDate=%s" "$f" "$(date +%FT%T)" > "$trashinfo"
        else
            lf -remote "send $id unselect"
        fi
    done
}}

# yank path
cmd yank-path &{{
    printf "%s" "$fx" | wl-copy
    lf -remote "send $id unselect"
}}

# media directory
cmd media-dir &{{
    if [ -n "$(ls -A /media)" ]; then
        lf -remote "send $id cd /media"
    elif [ -n "$(ls -A "/run/media/$(id -un)")" ]; then
        lf -remote "send $id cd /run/media/$(id -un)"
    fi
}}

# extract archive
cmd extract &{{
    for f in $fx; do
        case "$f" in
              *.tar.bz2 | *.tbz2) tar -xjf "$f" ;;
              *.tar.gz | *.tgz) tar -xzf "$f" ;;
              *.tar.xz | *.txz) tar -xJf "$f" ;;
              *.zip) unzip -nq "$f" -d "${f%.zip}" ;;
              *) lf -remote "send $id echoerr unsupported format" ;;
        esac
    done
    lf -remote "send $id unselect"
}}

# tgz archive
cmd tar &{{
    mkdir "$1"
    cp -r $fx "$1"
    tar -czf "$1.tar.gz" "$1"
    rm -rf "$1"
    lf -remote "send $id unselect"
}}

# zip archive
cmd zip &{{
    mkdir "$1"
    cp -r $fx "$1"
    zip -r "$1.zip" "$1"
    rm -rf "$1"
    lf -remote "send $id unselect"
}}

# change mode
cmd chmod &{{
    chmod "$1" $fx
    lf -remote "send $id unselect"
    lf -remote "send $id reload"
}}


# keymaps
map . set hidden!
map <enter> open
map p :paste; clear
map V invert-below
map R bulk-rename
map D trash
map <s-delete> delete
map E push $$EDITOR<space>
map a push %mkdir<space>-p<space>
map Y yank-path

# navigation
map ~ cd ~
map gd cd ~/Downloads
map gn cd ~/Documents/notes
map gc cd ~/.local/share/chezmoi
map gb cd ~/.local/bin
map gt cd ~/.local/share/Trash/files
map g/ cd /
map gr cd /
map ge cd /etc
map gm media-dir
